package com.rimi.lzhg.controller;


import com.rimi.lzhg.entry.Menu;
import com.rimi.lzhg.entry.TodayMenu;
import com.rimi.lzhg.entry.Type;
import com.rimi.lzhg.response.ResponseEntity;
import com.rimi.lzhg.service.MenuService;
import com.rimi.lzhg.service.TodayMenuService;
import com.rimi.lzhg.service.TypeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
public class TodayMenuController {

    @Autowired
    private TodayMenuService todayMenuService;

    @Autowired
    private TypeService typeService;

    @Autowired
    private MenuService menuService;

    @DeleteMapping("/manager/todayMenu")
    public Object deleteMenu(Integer id){
        if(id==null){
            return ResponseEntity.error("服务器删除数据失败:未成功传递ID");
        }
        boolean hasPeopleReserve = todayMenuService.isHasPeopleReserve(id);
        if(hasPeopleReserve){
            return ResponseEntity.error("服务器删除数据失败:已有用户下单过");
        }
        boolean isok=todayMenuService.deleteTodayMenuById(id);
        if(isok){
            return ResponseEntity.success("删除今日菜单成功");
        }
        return ResponseEntity.error("服务器删除数据失败");
    }

    @GetMapping("/manager/todayMenu")
    public Object getTodayMenu(TodayMenu todayMenu,Integer typeId) {
        //默认查看今日菜单
        if(todayMenu!=null&&todayMenu.getTime()==null){
            todayMenu.setTime(new Date());
        }
        //默认午餐
        if(todayMenu!=null&&todayMenu.getType()==null){
            todayMenu.setType(1);
        }
        List<Menu> menuList = todayMenuService.getMenuByDate(todayMenu, typeId);
        if(null==menuList){
            return ResponseEntity.error("今日暂无订单");
        }
        return ResponseEntity.success(menuList.size(),menuList);
    }

    @ApiOperation(value = "添加今日菜单",notes = "添加今日菜单，time为date类型 menuIds为 菜单编号Id的字符串 如：1,2,3")
    @PostMapping("/manager/todayMenu")
    public Object saveTodayMenuManager(TodayMenu todayMenu){
        //默认当前时间
        if(todayMenu!=null&&todayMenu.getTime()==null){
            todayMenu.setTime(new Date());
        }
        if(todayMenu!=null&&todayMenu.getType()==null){
            todayMenu.setType(1); //默认午餐
        }
        //检查今日是否重复添加
        TodayMenu todayMenu1 = todayMenuService.getMenuByDateAndType(todayMenu);
        if(todayMenu1!=null){
            return ResponseEntity.error("今日已添加过"+(todayMenu1.getType()==1?"午餐":"晚餐"));
        }
        if(todayMenuService.saveTodayMenu(todayMenu)){
            return ResponseEntity.success("添加今日菜单成功");
        }
        return ResponseEntity.error("服务器添加今日菜单失败");
    }

    @GetMapping("/manager/todayMenuHis")
    public Object getTodayMenuHis(TodayMenu todayMenu) {
        List<TodayMenu> todayMenuList = todayMenuService.getTodayMenuList(todayMenu);
        if(null==todayMenuList){
           return ResponseEntity.error("未查询到对应的数据");
        }
        return ResponseEntity.success(todayMenuList.size(),todayMenuList);
    }

    @ApiOperation(value = "根据日期查询今日菜单",notes = "根据日期查询今日菜单信息 菜单类型type  0早晚 1 午餐 2晚餐 可传递类型参数进行查询")
    @GetMapping("/todayMenu")
    public Object getTodayMenuList(TodayMenu todayMenu,Integer typeId){
        if(todayMenu!=null&&todayMenu.getTime()==null){
            todayMenu.setTime(new Date());
        }
        List<Type> typeList = typeService.getTypeList();
        Map<String,List> map=new HashMap();
        for (Type type : typeList) {
            map.put(type.getId().toString(),todayMenuService.getMenuByDate(todayMenu,type.getId()));
        }
        return map;
    }


    @ApiOperation(value = "添加今日菜单",notes = "添加今日菜单，time为date类型 menuIds为 菜单编号Id的字符串 如：1,2,3")
    @PostMapping("/todayMenu")
    public Object saveTodayMenu(TodayMenu todayMenu){
        //默认当前时间
        if(todayMenu!=null&&todayMenu.getTime()==null){
            todayMenu.setTime(new Date());
        }
        if(todayMenu!=null&&todayMenu.getType()==null){
            todayMenu.setType(1); //默认午餐
        }
        return todayMenuService.saveTodayMenu(todayMenu);
    }

    @ApiOperation(value = "查询所有今日菜单时间",notes = "查看所有今日菜单时间")
    @GetMapping("/todayMenuTime")
    public Object getAllTodayMenuTime(){
        return todayMenuService.getAllTodayMenuTime();
    }





}
